<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Model.php');
require_once($Config->WebCMSRoot.'Data/Form.php');

define('TEXTINPUT', 'TEXTINPUT');
define('TEXTAREA', 'TEXTAREA');
define('SUBMITBUTTON', 'SUBMITBUTTON');
define('PASSWORDINPUT', 'PASSWORDINPUT');
define('HIDDENINPUT', 'HIDDENINPUT');
define('FILEINPUT', 'FILEINPUT');
define('IMAGEINPUT', 'IMAGEINPUT');
define('RADIOBUTTON', 'RADIOBUTTON');
define('CHECKBOX', 'CHECKBOX');
define('BUTTON', 'BUTTON');
define('RESETBUTTON', 'RESETBUTTON');
define('SELECTBOX', 'SELECTBOX');

Class FormModel extends BaseModel
{
	var $Title;
	var $Name;
	var $Submit;
	var $Reset;
	
	var $VerificationFunction;
	var $Message;
	var $Errors;
	
	var $FormFields;

  var $IsComplete;
  
	function FormModel()
	{
		$this->Title  = '';
		$this->Name   = '';
		$this->Submit = '';
		$this->Reset  = '';
		
		$this->VerificationFunction = '';
		$this->Message = '';
		$this->Errors  = array();
		
		$this->FormFields = new FormFieldModel();

		$this->IsComplete = false;
		
		parent::BaseModel();
	}
	
	function Retrieve(&$parameters)
	{
		$db = new FormData();
		$this->Parameters = &$parameters;
		
		$this->ID   = $this->GetParameter('id');
		$this->Name = $this->GetParameter('name');
		
		$db->Retrieve(&$this);
	}

	function RetrieveFormFields(&$parameters)
	{
		$db = new FormFieldData();
		
		$this->FormFields->FormID = $this->ID;
//print('<pre>'.var_export($this,true).'</pre>');exit;
		
		$db->Retrieve(&$this);
	}

	function RetrieveFormValues(&$parameters)
	{
//print('<pre>'.var_export($parameters,true).'</pre>');//exit;
		$db = new FormEntryData();
		
		$this->FormFields->FormID = $parameters['ID'];
		
		$db->Retrieve(&$this);
	}

	function PersistEntries($formID)
	{
		$db = new FormEntryData();

		foreach($this->Collection[0]->FormFields->Collection as $entry)
		{
//print('<pre>'.var_export($entry,true).'</pre>');//exit;
			$db->Update(&$entry, $formID);
		}
	}
}

Class FormFieldModel extends BaseModel
{
	var $Label;
	var $Type;
	var $DefaultValue;
	var $Length;
	var $Required;
	var $Attributes;
	var $Validators;

	var $Name;
	var $SortOrder;
	var $Value;
	var $FormID;
	
	function FormField($label, $type, $formID, $name, $defaultValue='', $length=-1, $required=false, $attributes=array(), $validators=array())
	{
		$this->Label        = $label;
		$this->Type         = $type;
		$this->DefaultValue = $defaultValue;
		$this->Length       = $length;
		$this->Required     = $required;
		$this->Attributes   = $attributes;
		$this->Validators   = $validators;
		
		$this->Name         = $name;
		$this->FormID       = $formID;
		$this->Value        = '';
		$this->SortOrder    = 0;
	}
}

Class FormEntryModel extends BaseModel
{
	var $FormID;
	var $FormFieldID;
	var $Value;
	
	function FormEntryModel($formID, $formFieldID, $value)
	{
		$this->FormID      = $formID;
		$this->FormFieldID = $formFieldID;
		$this->Value       = $value;
	}
}
?>